from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
options = Options()
options.add_argument('--disable-infobars')
options.add_argument('--incognito')
# options.add_argument('--headless')
bro = webdriver.Chrome(executable_path='C:\drf2\drf2\chromedriver.exe',chrome_options=options)
bro.maximize_window()
bro.get('http://www.bdsmc.net/cloud/#/')
#定义隐式等待
bro.implicitly_wait(10)

def offline_rate():
    u = bro.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[3]/div[1]/div[1]/input')
    u.send_keys('ld_admin')
    # sleep(1)
    p = bro.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[3]/div[1]/div[2]/input')
    p.send_keys('123456')
    # sleep(1)

    # 点击登录按钮
    bro.find_element_by_class_name('el-button').click()
    # 点击确定按钮
    time.sleep(1)
    bro.find_element_by_xpath('//*[@id="app"]/div/div[2]/div/div[3]/button/span').click()
    # 点击统计分析
    fm = bro.find_element_by_xpath('//*[@id="app"]/div/div[2]/div[1]/div/div/div/div/div/div/div[2]/div[1]/div[3]/a/div')
    actions = ActionChains(bro)
    actions.move_to_element(fm).click().perform()
    #展开侧边栏
    bro.find_element_by_xpath('//*[@id="app"]/section/section/aside/div[2]/div[1]/div').click()
    time.sleep(1)
    #展开左侧菜单
    menu_list = bro.find_elements_by_xpath('//*[@id="app"]/section/section/aside/div[2]/div[3]/div[1]/div/div/div[position() < last() ]')
    # bro.find_element_by_xpath('//*[@id="app"]/section/section/aside/div[2]/div[3]/div[1]/div/div/div[2]').click()
    print('左侧菜单长度',len(menu_list))
    for menu in menu_list:
        # actions.move_to_element(menu).click().perform()
        time.sleep(2)
        menu.click()
    red_alerts = bro.find_elements_by_xpath('//span//*[name()="svg" and @style="color: rgb(255, 109, 74);"]/parent::*')
    # yellow_alerts = bro.find_elements_by_xpath('//span//*[name()="svg" and @style="color: rgb(247, 186, 42);"]/parent::*')
    red_alerts_list = []
    yellow_alerts = []
    def get_warnings(alerts,alerts_list):
        # print('alerts',alerts)
        for alert in alerts:
            # print('alert',alert.text)
            alerts_list.append(alert.text.split(' ')[2])
    get_warnings(red_alerts,red_alerts_list)
    print(red_alerts_list)
    time.sleep(1)
    bro.quit()

offline_rate()